package com.itheima.mall.api.ums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ums.AdminMapper;
import com.itheima.mall.mapper.ums.UmsAdminRoleRelationMapper;
import com.itheima.mall.pojo.ums.Admin;
import com.itheima.mall.pojo.ums.UmsAdminRoleRelation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
@Slf4j
public class AdminServiceImpl implements AdminServiceApi {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    @Override
    public Admin findByName(String username) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 查找用户信息
     * @param adminId
     * @return
     */
    @Override
    public Admin info(Long adminId) {
        Admin admin = adminMapper.selectById(adminId);
        return admin;
    }

    /**
     * 根据id删除用户
     * @param id
     */
    @Override
    public void delAdmin(Long id) {
        adminMapper.deleteById(id);
    }

    /**
     * 分页模糊查询用户
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(String keyword, Integer pageNum, Integer pageSize) {
        Page pageInfo = new Page(Convert.toLong(pageNum),Convert.toLong(pageSize));
        if(StrUtil.isNotEmpty(keyword)){
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Admin::getUsername, keyword);
            Page page1 = adminMapper.selectPage(pageInfo, queryWrapper);
            log.error("Total:{}",page1.getTotal());
            if(page1.getTotal() != 0){
                return page1;
            }else {
                LambdaQueryWrapper<Admin> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.like(Admin::getNickName,keyword);
                page1 = adminMapper.selectPage(pageInfo, queryWrapper);
                return page1;
            }
        }else {
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            Page page1 = adminMapper.selectPage(pageInfo, queryWrapper);
            return page1;
        }
    }

    /**
     * 用户注册
     * @param admin
     */
    @Override
    public void register(Admin admin) {
        adminMapper.insert(admin);
    }

    /**
     * 涉及ums_admin_role_relation和ums_role
     * 给用户分配角色
     * @param adminId
     * @param roleId
     */
    @Override
    public void roleUpdate(Long adminId, Long[] roleId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId,adminId);
        List<UmsAdminRoleRelation> umsAdminRoleRelationList = umsAdminRoleRelationMapper.selectList(queryWrapper);
        List<Long> ids = CollUtil.getFieldValues(umsAdminRoleRelationList, "id", Long.class);
        umsAdminRoleRelationMapper.deleteBatchIds(ids);
        for (Long aLong : roleId) {
            UmsAdminRoleRelation umsAdminRoleRelation = new UmsAdminRoleRelation();
            umsAdminRoleRelation.setAdminId(adminId);
            umsAdminRoleRelation.setRoleId(aLong);
            umsAdminRoleRelationMapper.insert(umsAdminRoleRelation);
        }
    }

    @Override
    public Admin getUserInfoById(Long id) {
        return adminMapper.selectById(id);
    }

    @Override
    public void updateUserInfoById(Admin admin) {
        adminMapper.updateById(admin);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }

}
